package com.mx.project.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConstants;
import com.alipay.api.internal.util.AlipaySignature;
import com.mx.project.common.resp.ApiResult;
import com.mx.project.common.util.JWTService;
import com.mx.project.constants.AliPayConstants;
import com.mx.project.pojo.Order;
import com.mx.project.services.CartService;
import com.mx.project.services.OrderService;
import com.mx.project.vo.CartInfo;
import com.mx.project.vo.OrderInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private JWTService jwtService;

    @Autowired
    private CartService cartService;

    @ApiOperation("支付宝支付接口")
    @PostMapping(value = "/toPay")
    public String alipay(@RequestBody List<CartInfo> lists, HttpServletRequest request) throws AlipayApiException {
        String token = request.getHeader("token");
        Integer uid = jwtService.getUserId(token);
        String response = orderService.aliPay(lists, 19);
        return response;
    }

    @GetMapping()
    public ApiResult getOrder(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer uid = jwtService.getUserId(token);
        ApiResult result = new ApiResult();
        List<OrderInfo> list = orderService.getOrderByUid(uid);
        result.setData(list);
        return result;
    }

    @RequestMapping("notify")
    public void test(HttpServletRequest request) throws AlipayApiException {
        //获取支付宝POST过来反馈信息
        Map<String, String> maps = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"),"utf-8");
            maps.put(name, valueStr);
        }
        String out_trade_no = maps.get("out_trade_no");
        List<Order> list = orderService.getOrderByOnumber(out_trade_no);
        Integer uid = list.get(0).getUid();
        cartService.removeCartByUid(uid);
        int count = orderService.updateStateByOnumber(out_trade_no,1);
        boolean signVerified = AlipaySignature.rsaCheckV1(maps, AliPayConstants.alipay_public_key, "UTF-8", "RSA2");
        if(!signVerified){
            return;
        }
    }
}
